package com.paw.ruoyi.admin.common.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.paw.ruoyi.admin.common.annotion.Excel;

/**
 * @author lvzihai
 * @date 2022/1/24
 **/
public class ExcelHeaderAliasUtil {

  /**
   * 获取Excel bean的别名
   * @param clazz
   * @return
   */
  public static List<HeaderAliasDto> getHeaderAlias (Class clazz) {
    List<HeaderAliasDto> list = new ArrayList<>();
    List<Field> tempFields = new ArrayList<>();
    tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
    tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));

    for (Field field : tempFields) {
      if (field.isAnnotationPresent(Excel.class)) {
        Excel excel = field.getAnnotation(Excel.class);
        if (excel != null) {
          field.setAccessible(true);
          list.add(new HeaderAliasDto(field.getName(), excel.name(), excel.sort()));
        }
      }
    }
    return list;
  }

  public static class HeaderAliasDto{
    private String field;
    private String alias;
    private Integer sort;


    public HeaderAliasDto () {
    }

    public HeaderAliasDto (String field, String alias, Integer sort) {
      this.field = field;
      this.alias = alias;
      this.sort = sort;
    }

    public String getField () {
      return field;
    }

    public void setField (String field) {
      this.field = field;
    }

    public String getAlias () {
      return alias;
    }

    public void setAlias (String alias) {
      this.alias = alias;
    }

    public Integer getSort () {
      return sort;
    }

    public void setSort (Integer sort) {
      this.sort = sort;
    }
  }



}
